package com.mlizhi.modules.splash;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.mlizhi.base.Session;
import com.mlizhi.kingdom.R;
import com.mlizhi.modules.login.AutoLoginActivity;
import com.mlizhi.modules.login.LoginActivity;
import com.mlizhi.modules.spec.SpecActivity;
import com.mlizhi.utils.Constants;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.analytics.MobclickAgent;

import java.util.Calendar;

public class PreSettingActivity extends Activity implements OnPageChangeListener {
	/**
	 * ViewPager
	 */
	private ViewPager viewPager;

	/**
	 * 装点点的ImageView数组
	 */
	private ImageView[] tips;

	/**
	 * 装ImageView数组
	 */
	private View[] mSplashViews;

	/**
	 * 图片资源id
	 */
	// private int[] imgIdArray ;

	private Context mContext;

	private LayoutInflater mInflater;

	private Session mSession;

	// private ImageView splashImage;

//	private MlzApplication mlzApplication;
	// 日期选择器
	private DatePicker datePicker = null;
	// 性别group
	private RadioGroup sexGroup;
	// 肤质类型group
	private RadioGroup skinTypeGroup;
	
	private String userSex = Constants.USER_SEX_FEMALE;
	private String userBriday ="1995-01-01";
	private String userSkinType = Constants.USER_SKIN_TYPE_UNKNOW;

	@SuppressLint("InflateParams")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.mContext = this;
		mSession = Session.get(getApplicationContext());
		/** 发送策略定义了用户由统计分析SDK产生的数据发送回友盟服务器的频率。 */
		MobclickAgent.updateOnlineConfig( mContext );
		/** 设置是否对日志信息进行加密, 默认false(不加密). */
		AnalyticsConfig.enableEncrypt(true);
//		mlzApplication = (MlzApplication) getApplication();
		String sex = mSession.getUserSex();
		String briday = mSession.getUserBriday();
		String skinType = mSession.getUserSkinType();
		if ((sex != null && !"".equals(sex))
				&& (briday != null && !"".equals(briday))
				&& (skinType != null && !"".equals(skinType))) {
			String uid = mSession.getUid();
			if (uid != null && !"".equals(uid)) {
				mContext.startActivity(new Intent(mContext,AutoLoginActivity.class));
			} else {
				mContext.startActivity(new Intent(mContext, LoginActivity.class));
			}
			finish();
		} else{
			this.mInflater = LayoutInflater.from(mContext);
			setContentView(R.layout.activity_pre_setting);
			ViewGroup group = (ViewGroup) findViewById(R.id.pre_setting_viewGroup);
			viewPager = (ViewPager) findViewById(R.id.pre_setting_viewPager);
			
			// 将图片装载到数组中
			mSplashViews = new View[3];
			// *********预先设置出生年月 begin *******
			mSplashViews[0] = mInflater.inflate(R.layout.view_pre_setting_item_age, null);
			datePicker = (DatePicker) mSplashViews[0].findViewById(R.id.pre_setting_birthday);
			Calendar cal = Calendar.getInstance();
			cal.set(1995, 0, 1);
//			cal.setTimeInMillis(System.currentTimeMillis());
			datePicker.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH), new OnDateChangedListener() {
				
				@Override
				public void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
					userBriday = "" + year + "-"+ (monthOfYear + 1) + "-" + dayOfMonth;
				}
			});
			// *********预先设置出生年月 end *******
			// *********预先设置性别begin *******
			mSplashViews[1] = mInflater .inflate(R.layout.view_pre_setting_item_sex, null);
			sexGroup = (RadioGroup) mSplashViews[1] .findViewById(R.id.user_pre_setting_sex_group);
			sexGroup.check(R.id.user_pre_setting_sex_female);
			sexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch (checkedId) {
					case R.id.user_pre_setting_sex_female:
//					mSession.setUserSex(userSex);
						userSex = Constants.USER_SEX_FEMALE;
						break;
					case R.id.user_pre_setting_sex_male:
						userSex = Constants.USER_SEX_MALE;
						break;
					default:
						break;
					}
				}
			});
			// *********预先设置性别end *******
			// *********预先设置肤质类型end *******
			mSplashViews[2] = mInflater.inflate(R.layout.view_pre_setting_item_skan_type, null);
			skinTypeGroup = (RadioGroup) mSplashViews[2] .findViewById(R.id.user_pre_setting_skin_type_group);
			skinTypeGroup.check(R.id.user_pre_setting_skin_type_unknow);
			skinTypeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					switch (checkedId) {
					case R.id.user_pre_setting_skin_type_dry:
						userSkinType = Constants.USER_SKIN_TYPE_DRY;
						break;
					case R.id.user_pre_setting_skin_type_oil:
						userSkinType = Constants.USER_SKIN_TYPE_OILY;
						break;
					case R.id.user_pre_setting_skin_type_sensitive:
						userSkinType = Constants.USER_SKIN_TYPE_SENSITIVE;
						break;
					case R.id.user_pre_setting_skin_type_hybird:
						userSkinType = Constants.USER_SKIN_TYPE_HYBRID;
						break;
					case R.id.user_pre_setting_skin_type_unknow:
						userSkinType = Constants.USER_SKIN_TYPE_UNKNOW;
						break;
						
					default:
						break;
					}
				}
			});
			
			mSplashViews[2].findViewById(R.id.pre_setting_certain).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					mSession.setSplashId(1);
					mSession.setUserBriday(userBriday);
					mSession.setUserSex(userSex);
					mSession.setUserSkinType(userSkinType);
					mContext.startActivity(new Intent(mContext, SpecActivity.class));
					finish();
				}
			});
			// *********预先设置肤质类型end *******
			// 将点点加入到ViewGroup中
			tips = new ImageView[mSplashViews.length];
			for (int i = 0; i < tips.length; i++) {
				
				ImageView imageView = new ImageView(this);
				imageView.setLayoutParams(new LayoutParams(10, 10));
				tips[i] = imageView;
				if (i == 0) {
					tips[i].setBackgroundResource(R.drawable.ic_splash_indicator_focused);
				} else {
					tips[i].setBackgroundResource(R.drawable.ic_splash_indicator);
				}
				
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
						new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
								LayoutParams.WRAP_CONTENT));
				layoutParams.leftMargin = 5;
				layoutParams.rightMargin = 5;
				layoutParams.bottomMargin = 30;
				group.addView(imageView, layoutParams);
			}
			
			// 设置Adapter
			viewPager.setAdapter(new MyAdapter());
			// 设置监听，主要是设置点点的背景
			viewPager.setOnPageChangeListener(this);
			// 设置ViewPager的默认项, 设置为长度的100倍，这样子开始就能往左滑动
			viewPager.setCurrentItem(0);
		}
		

	}

	public class MyAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			return mSplashViews.length;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public void destroyItem(View container, int position, Object object) {
			((ViewPager) container).removeView(mSplashViews[position
					% mSplashViews.length]);

		}

		/**
		 * 载入图片进去，用当前的position 除以 图片数组长度取余数是关键
		 */
		@Override
		public Object instantiateItem(View container, int position) {
			((ViewPager) container).addView(mSplashViews[position
					% mSplashViews.length], 0);
			return mSplashViews[position % mSplashViews.length];
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		setImageBackground(arg0 % mSplashViews.length);
	}

	/**
	 * 设置选中的tip的背景
	 * 
	 * @param selectItems
	 */
	private void setImageBackground(int selectItems) {
		for (int i = 0; i < tips.length; i++) {
			if (i == selectItems) {
				tips[i].setBackgroundResource(R.drawable.ic_splash_indicator_focused);
			} else {
				tips[i].setBackgroundResource(R.drawable.ic_splash_indicator);
			}
		}
	}

	
	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("SplashScreen"); //统计页面(仅有Activity的应用中SDK自动调用，不需要单独写)
	    MobclickAgent.onResume(this);          //统计时长
	}

	@Override
	public void onPause() {
		super.onPause();
		 MobclickAgent.onPageEnd("SplashScreen"); // （仅有Activity的应用中SDK自动调用，不需要单独写）保证 onPageEnd 在onPause 之前调用,因为 onPause 中会保存信息 
		    MobclickAgent.onPause(this);
	}
	
}
